package com.hndzyu.yd.home;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.Gbean;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.utils.Util;

import java.util.List;

/**
 * Created by zz9527 on 2018/12/4.
 */
public class GamedetailAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private List<Gbean> mList;
    private final Context context;

    public GamedetailAdapter(Context context, List<Gbean> list) {
        this.context = context;
        this.mList = list;
        inflater = LayoutInflater.from(context);
    }

    public void setData(List<Gbean> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Gbean getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.lv_item_task_desc, null);
            viewHolder.tv_id = (TextView) convertView.findViewById(R.id.lvitem_tv_ext_id);
            viewHolder.tv_desc = (TextView) convertView.findViewById(R.id.lvitem_tv_ext_desc);
            viewHolder.tv_status = (TextView) convertView.findViewById(R.id.lvitem_tv_ext_status);
            viewHolder.tv_points = (TextView) convertView.findViewById(R.id.lvitem_tv_ext_points);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Gbean bean = mList.get(position);

        // 设置左边的数字序号
        viewHolder.tv_id.setText(String.valueOf(position + 1));
        // 设置任务赚取描述
        try {
            viewHolder.tv_desc.setText(bean.getTaskContent());

        // 设置积分或者已完成
//        if(bean.getMtTaskFee()!=null){
            if (UserBean.level>0 ){
                viewHolder.tv_points.setText("+" + Util.Sub(Double.parseDouble(bean.getMtTaskFee())*0.9*(Double.parseDouble(UserBean.vipdivide)+Double.parseDouble(UserBean.divide))+"")+"元");
            }else{
                viewHolder.tv_points.setText("+" + Util.Sub(Double.parseDouble(bean.getMtTaskFee())*0.9*Double.parseDouble(UserBean.divide)+"")+"元");
            }
        }catch (Exception e){}

//        }else {
//            viewHolder.tv_points.setText("数据异常");
//        }

        Drawable rightDrawable = context.getResources().getDrawable(R.mipmap.share);
        rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
        switch (bean.getState()) {
            // 任务可以做
            case 0:
            case 1:
//                viewHolder.tv_points.setCompoundDrawables(rightDrawable, null, null, null);
//                viewHolder.tv_points.setBackgroundResource(R.drawable.bg_money);
                viewHolder.tv_points.setTextColor(context.getResources().getColor(R.color.ff9d0f));
                viewHolder.tv_status.setText("任务可进行");
                break;
            // 任务已完成
            case 2:
                viewHolder.tv_points.setTextColor(context.getResources().getColor(R.color.red));
                viewHolder.tv_status.setText("任务已完成");
//                viewHolder.tv_points.setCompoundDrawables(null, null, null, null);
//                viewHolder.tv_points.setBackgroundResource(0);
                break;
//            case 2:
//                viewHolder.tv_points.setTextColor(context.getResources().getColor(R.color.color_huise));
//                viewHolder.tv_status.setText("任务已完成");
//                viewHolder.tv_points.setText("过期");
//                viewHolder.tv_points.setCompoundDrawables(null, null, null, null);
//                viewHolder.tv_points.setBackgroundResource(0);
//                break;
            default:
                break;
        }

        return convertView;
    }

    private static class ViewHolder {
        TextView tv_id;
        TextView tv_desc;
        TextView tv_status;
        TextView tv_points;
    }
}